<?php

// no direct access
defined('_JEXEC') or die('Restricted access');

// check evolve_library is installed
if (!file_exists(JPATH_PLUGINS.'/system/evolvelibrary.php')) {
	
	// can't do much without it
	jimport('joomla.error.log');
	
	$errorLog = 'syncr-error_log-'.date('Y-m-d').'.log.php';
    $options = array('format' => "{DATE}\t{TIME}\t{COMMENT}");
    
    // Create the instance of the log file in case we use it later
    $log = &JLog::getInstance($errorLog, $options);
    $log->addEntry(array('comment' => 'evolve library not loading'));
	
	JError::raiseWarning(404, JText::_('JEVOLVE_LIBRARY_MISSING'));
	return;
}

// load evolve_library
require_once JPATH_PLUGINS.'/system/evolvelibrary.php';

$evl = new evolveLibrary;

// check evolve_library is enabled
if (!$evl->enabled()) {
	// can't do much without it
	JError::raiseWarning(500, JText::_('JEVOLVE_LIBRARY_DISABLED'));
	return;
}

// enabled logging
$log = $evl->loadHelper('log');

// load tables
JTable::addIncludePath(JPATH_COMPONENT.'/tables');

// get the base controller or use default
$controller = JRequest::getCmd('controller', 'about');

// define the controller name and path
$controllerName = strtolower($controller);
$controllerPath = JPATH_COMPONENT.'/controllers/'.$controllerName.'.php';

if (file_exists($controllerPath)) {
	require_once $controllerPath;
} else {

	$log->simpleLog('admin/syncr.php: invalid controller', 'syncr');
	JError::raiseError(500, 'SYNCR_INVALID_CONTROLLER');
}

// create controller name
$controllerName = 'SyncrController'.ucfirst($controller);

// create controller instance
if (class_exists($controllerName)) {
	$controller = new $controllerName();
} else {
	$log->simpleLog('admin/syncr.php: invalid controller class', 'syncr');
	JError::raiseError('500', 'SYNCR_INVALID_CONTROLLER_CLASS');
}

// perform the requested task
$controller->execute(JRequest::getCmd('task'));
$controller->redirect();